package com.igr.media.security;

import com.igr.media.dto.RegisterReq;
import com.igr.media.dto.Role;
import com.igr.media.entity.UserEntity;
import com.igr.media.exception.ElemNotFound;
import com.igr.media.loger.FormLogInfo;
import com.igr.media.repository.UserRepository;
import com.igr.media.utils.Encoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service("UserDetailServiceImpl")
@Slf4j
public class UserDetailService implements UserDetailsService {

  private final UserRepository userRepository;

  public UserDetailService(UserRepository userRepository) {
    this.userRepository = userRepository;
  }

  /**
   * Аутентификация пользователя
   */
  public boolean login(String email, String password) {
    log.info(FormLogInfo.getInfo());
    UserEntity userEntity = userRepository.findByEmail(email).orElse(null);
    if (userEntity == null) {
      return false;
    }
    String encryptedPassword = userEntity.getPassword();
    return Encoder.passwordEncoderWithBCrypt().matches(password, encryptedPassword);
  }

  /**
   * Сетим поля ентити из запроса для сохранения в таблицу, проверяем на нуль
   */
  public boolean register(RegisterReq registerReq) {
    log.info(FormLogInfo.getInfo());
    UserEntity userEntity = userRepository.findByEmail(registerReq.getUsername()).orElse(null);
    if (userEntity != null) {
      return false;
    }
    userEntity = new UserEntity();
    userEntity.setName(registerReq.getName());
    userEntity.setData(LocalDateTime.now());
    userEntity.setEmail(registerReq.getUsername());
    userEntity.setImage(
        "iVBORw0KGgoAAAANSUhEUgAAAYwAAAFFCAIAAABwvE1hAAAzP0lEQVR42u2de3xU5Z3/nzO5MLlOCLkB4RYwIdykWIFC5KICAhIEFEvXdm3drm5dq61au922Umttt66+6toLVumv2rpWwAJBhVpUcIFSrQgEJIlcBIFAEhJCEnKbzPmdmTNz5jnXOTNzwpxn5vP+Q8+cec5lJs7b53nmme+Hq5g9lzCIY/x0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C");
    userEntity.setPassword(Encoder.passwordEncoderWithBCrypt().encode(registerReq.getPassword()));
    userEntity.setRole(Role.USER);
    userRepository.save(userEntity);
    return true;
  }

  /**
   * передаем пароль и почту для настройки {@link com.igr.media.WebSecurityConfig #daoAuthenticationProvider() }
   */
  public UserDetails loadUserByUsername(String email) {
    UserEntity userEntity = userRepository.findByEmail(email)
        .orElseThrow(ElemNotFound::new);
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    if (userEntity.getRole().equals(Role.ADMIN)) {
      authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
    }

    return new User(userEntity.getEmail(), userEntity.getPassword(), authorities);
  }


}
